using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.Mail;
using Entity;
using ECommerce.DAL;
using System.Collections.Specialized;
using ECommerce.Library;
using System.Web;
using ECommerce.Library.DataStructure;

namespace AutoGear.BLL
{
    public class Newsletter
    {
        private int _newsletterId;
        private string _subject;
        private string _body;
        private DateTime _date;


        private ReadWriteCustomCollection<NewsletterSubscriber> _subscribers;

        public ReadWriteCustomCollection<NewsletterSubscriber> Subscribers
        {
            get { return _subscribers; }
            set { _subscribers = value; }
        }


        public int NewsletterId
        {
            get { return _newsletterId; }
            set { _newsletterId = value; }
        }

        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }
        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }




        public Newsletter(int newsletterId, string subject, string body, DateTime date)
        {
            this._newsletterId = newsletterId;
            this._subject = subject;
            this._body = body;
            this._date = date;
            this._subscribers = new ReadWriteCustomCollection<NewsletterSubscriber>();
        }


        public Newsletter(string subject, string body)
        {
            this._subject = subject;
            this._body = body;
            this._subscribers = new ReadWriteCustomCollection<NewsletterSubscriber>();
        }

        public static Newsletter Load(int newsletterId)
        {
            TblNewsletterArchive tmpTblNewsletterArchive = TblNewsletterArchive.GetTblNewsletterArchiveByNewsletterArchiveId(newsletterId);
            Newsletter newsletter = new Newsletter(tmpTblNewsletterArchive.NewsletterArchiveId, tmpTblNewsletterArchive.Subject, tmpTblNewsletterArchive.Body, tmpTblNewsletterArchive.Date);
            return newsletter;
        }


        public static bool SendNewsletter(Newsletter newsletter)
        {
            string fulltext = string.Empty;
            bool status = Email.SendNewsletterEmail(newsletter, out fulltext);
            newsletter.Body = fulltext;
            return status;

        }

        public static string PreviewNewsletter(Newsletter newsletter)
        {
            return Email.PreviewNewsletterEmail(newsletter);
        }

        public static bool Create(Newsletter newsletter)
        {
            TblNewsletterArchive tblNewsletter = new TblNewsletterArchive();
            tblNewsletter.Body = newsletter.Body;
            tblNewsletter.Date = DateTime.Now;
            tblNewsletter.Subject = newsletter.Subject;
            return tblNewsletter.Save();
        }

        public static int TotalNewsletterCount()
        {
            int count = TblNewsletterArchive.GetAllTblNewsletterArchivePagedCount("", 0, 99999);
            return count;
        }

        public static ReadWriteCustomCollection<Newsletter> Newsletters(string sortBy, int firstItemPosition, int pageSize, bool showAll)
        {
            ReadWriteCustomCollection<Newsletter> newsletters;

            if (showAll)
            {
                newsletters = LoadFromCustomCollection(TblNewsletterArchive.GetAllTblNewsletterArchivePaged(sortBy, 1, 99999));
            }
            else
            {
                newsletters = LoadFromCustomCollection(TblNewsletterArchive.GetAllTblNewsletterArchivePaged(sortBy, firstItemPosition, pageSize));
            }
            return newsletters;
        }

        private static ReadWriteCustomCollection<Newsletter> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblNewsletterArchive> tblNewsletterArchive)
        {
            ReadWriteCustomCollection<Newsletter> result = new ReadWriteCustomCollection<Newsletter>();
            for (int i = 0; i < tblNewsletterArchive.Count; i++)
            {
                TblNewsletterArchive tmpTblNewsletterArchive = tblNewsletterArchive[i];
                result.Add(new Newsletter(tmpTblNewsletterArchive.NewsletterArchiveId, tmpTblNewsletterArchive.Subject, tmpTblNewsletterArchive.Body, tmpTblNewsletterArchive.Date));
            }
            return result;
        }



    }
}
